library hz_dialog;

import 'package:flutter/material.dart';
import 'hz_dialog_action.dart';
import 'hz_dialog_action_item.dart';

//默认 title 样式
const defaultTitleStyle = TextStyle(
    color: Color(0xFF3333333), fontSize: 18, fontWeight: FontWeight.w600);
//默认 内容主题 样式
const defaultContentStyle = TextStyle(color: Color(0xFF3333333), fontSize: 17);

const Size HZDialogSize = Size(270, 75);
///
/// show custom dialog
/// [title] the top content of dialog; limited:[Text.maxLines]=3
/// [content] the mid content of dialog (required)
/// [actions] The Actions list that will appear on the Dialog. (required)
///
Future<T?> showHZDialog<T>({
  required BuildContext context,
  Widget? title,
  Widget? content,
  required List<HZDialogAction> actions,
}) {
  Size contentSize = MediaQuery.of(context).size;
  return showDialog(
    context: context,
    barrierDismissible: false,
    builder: (context) {
      onTap(callback) {
        Navigator.of(context).pop();
        if (callback != null) callback();
      }

      return Material(
        type: MaterialType.transparency,
        child: Center(
          child: ClipRRect(
            borderRadius: BorderRadius.all(Radius.circular(10)),
            child: Container(
              constraints: BoxConstraints(
                maxWidth: HZDialogSize.width,//contentSize.width * 0.8,
              ),
              color: Colors.white,
              child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.stretch,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  if (title != null)
                    Padding(
                      padding: EdgeInsets.only(left: 15, right: 15, top: 10),
                      child: DefaultTextStyle(
                        maxLines: 3,
                        textAlign: TextAlign.center,
                        overflow: TextOverflow.ellipsis,
                        style: defaultTitleStyle,
                        child: title,
                      ),
                    ),
                  ConstrainedBox(
                    constraints: BoxConstraints(
                      maxHeight: contentSize.height * 0.4,
                      minHeight: HZDialogSize.height, //ScreenUtil().setWidth(70)
                    ),
                    child: SingleChildScrollView(
                      child: DefaultTextStyle(
                        style: defaultContentStyle,
                        child: Padding(
                          padding: EdgeInsets.symmetric(
                              horizontal: 15, vertical: 10),
                          child: content,
                        ),
                      ),
                    ),
                  ),
                  if (actions.length > 0)
                    Divider(
                      thickness: 0.5,
                      indent: 0,
                      endIndent: 0,
                      color: Color(0xFFE2E2E2),
                      height: 1,
                    ),
                  if (actions.length > 0)
                    Row(
                      mainAxisSize: MainAxisSize.min,
                      children: actions.map((action) {
                        int index = actions.indexOf(action);
                        return Expanded(
                          flex: 1,
                          child: HZDialogActionItem(
                            action: action,
                            onTap: () => onTap(action.callback),
                          ),
                        );
                      }).toList(),
                    ),
                ],
              ),
            ),
          ),
        ),
      );
    },
  );
}
